use core::client::{Client, NetconfClient};
use std::sync::Arc;

use tokio::{select, spawn, sync::mpsc::channel};
use yang2::{
    context::{Context, ContextFlags},
    Error,
};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let (tx, rx) = channel::<String>(100);
    let (notify_sender,mut notify_recvier) = channel(100);
    let mut context = Context::new(ContextFlags::ALL_IMPLEMENTED)?;
    context.set_searchdir("./yang").expect("can not load yang files");
    let ctx = Arc::new(context);
    let mut client = Client::new(("localhost", 8300), "dev", "dev", tx, notify_sender, ctx);
    client.connect(rx).await.expect("connect failed");
    spawn(async move {
        loop {
            select! {
                notify = notify_recvier.recv() => {
                    if let Some(msg) = notify {
                        println!("recv notify msg: {:?}", msg);
                    }
                }
                _ = tokio::signal::ctrl_c() => {
                    break;
                }
            }
        }
    });
    let nes = client.get_xpath("/ne:nes[ip='10.2.0.2']/ip").await.expect("get nes data");
    println!("get nes: {:?}", nes);
    Ok(())
}
